var gulp = require('gulp'),
	htmlreplace = require('gulp-html-replace'),
	merge = require('merge-stream'),
	gulpLoadPlugins = require('gulp-load-plugins'),
	plugins = gulpLoadPlugins();


// 结果地址
var DIST_PATH = '../../dist/';

/**
 * 避免冲突(特殊用途)
 * 全部静态资源清理一下
 * gulp common.del.all
 */
gulp.task('common.del.all',function(){
	return gulp.src([DIST_PATH])
		.pipe(plugins.clean({ force: true }));
});

/**
 * 了解已有的plugins
 */
gulp.task('common.test.plugins',function(){
	console.log(plugins);
});

/**
 * CSS文件的处理
 * 1、压缩CSS，并且合并相应的CSS文件；
 * 2、对CSS进行MD5编号；
 * 3、将CSS文件释放到指定的地方；
 */
gulp.task('common.all.css',function(){
	// 全局CSS
	var common = gulp.src('../../source/dist/css/common/*')
		.pipe(plugins.concat('plugins.common.min.css'));

	// 内部业务CSS
	var dev = gulp.src('../../source/dist/css/dev/*')
		.pipe(plugins.concat('plugins.dev.min.css'));

	// 模板原始CSS
	var lte = gulp.src('../../source/dist/css/AdminLTE.css')
		.pipe(plugins.concat('plugins.lte.min.css'));

	return merge([common, dev, lte])
		.pipe(plugins.minifyCss())
		.pipe(plugins.rev())
		.pipe(gulp.dest(DIST_PATH + '/css'))
		.pipe(plugins.rev.manifest({merge: true}))
		.pipe(gulp.dest(DIST_PATH + '/rev/css'));
});

/**
 * js文件的处理
 * 1、压缩JS文件，并且合并相应的JS文件；
 * 2、对JS文件进行MD5编号
 * 3、将JS文件释放到指定的地方
 */
gulp.task('common.all.js',function(){
	var JSPATH = '../../source/dist/js';

	// 特殊插件
	var special = gulp.src([
			JSPATH + '/common/special/jquery/jquery.js',
			JSPATH + '/common/special/select/select.js',
			JSPATH + '/common/special/tables/bootstrap.table.js'
		])
		.pipe(plugins.concat('plugins.special.min.js'));

	var canvas = gulp.src(JSPATH + '/common/special/canvas/canvas.js')
		.pipe(plugins.concat('plugins.canvas.min.js'));

	// 公共(插件)
	var common = gulp.src(JSPATH + '/common/plugins/**/*')
		.pipe(plugins.concat('plugins.common.min.js'));

	// 逻辑
	var dev = gulp.src(JSPATH + '/dev/**/*')
		.pipe(plugins.concat('plugins.dev.min.js'));

	// 工具插件（自研／第三方）
	var public = gulp.src(JSPATH + '/public/**/*')
		.pipe(plugins.concat('plugins.public.min.js'));

	// 全局参数（方便模块化）
	var glob = gulp.src(JSPATH + '/glob.js')
		.pipe(plugins.concat('plugins.glob.min.js'));

	// main 的 JS
	var index = gulp.src(JSPATH + '/index.js')
		.pipe(plugins.concat('plugins.index.min.js'));

	// login 的 JS
	var login = gulp.src(JSPATH + '/login.js')
		.pipe(plugins.concat('plugins.login.min.js'));

	return merge([special, common, dev, public, glob, index, login, canvas])
		.pipe(plugins.uglify({mangle:true}))
		.pipe(plugins.rev())
		.pipe(gulp.dest(DIST_PATH + '/js'))
		.pipe(plugins.rev.manifest({ merge: true }))
		.pipe(gulp.dest(DIST_PATH + '/rev/js'));
});

/**
 * login/main页面的构建
 * 1、静态资源的替换，模版渲染。
 * 2、字符配置的移动。
 */
gulp.task('common.all.php',['common.all.css','common.all.js'],function(){
	// 模版格式化（login）
	var login_php = gulp.src('../../source/template/login.html')
		.pipe(htmlreplace({
			gcss: {
				src: [
					'dist/css/plugins.common.min.css',
					'dist/css/plugins.lte.min.css',
				],
				tpl: '<link rel="stylesheet" href="<?=base_url().\'%s\'?>">'
			},
			loginjs: {
				src: [
					'dist/js/plugins.special.min.js',
					'dist/js/plugins.common.min.js',
					'dist/js/plugins.public.min.js',
					'dist/js/plugins.glob.min.js',
					'dist/js/plugins.dev.min.js',
					'dist/js/plugins.login.min.js',
				],
				tpl: '<script src="<?=base_url().\'%s\'?>"></script>'
			},
			canvas: {
				src: [
					'dist/js/plugins.canvas.min.js'
				],
				tpl: '<script color="0,0,0" opacity="0.6" zIndex="-2" count="100" src="<?=base_url().\'%s\'?>"></script>'
			}
		}))
		.pipe(plugins.rename('login.php'))
		.pipe(gulp.dest(DIST_PATH + '.standby/'));

	// 模板格式化（main）
	var imgsrc = ['dist/img/user1-128x128.jpg'];
	var frame_php = gulp.src('../../source/template/frame.html')
		.pipe(htmlreplace({
			gcss: {
				src: [
					'dist/css/plugins.common.min.css',
					'dist/css/plugins.dev.min.css',
				],
				tpl: '<link rel="stylesheet" href="<?=base_url().\'%s\'?>">'
			},
			pluginjs: {
				src: [
					'dist/js/plugins.special.min.js',
					'dist/js/plugins.common.min.js',
					'dist/js/plugins.public.min.js',
					'dist/js/plugins.glob.min.js',
					'dist/js/plugins.dev.min.js',
					'dist/js/plugins.index.min.js'
				],
				tpl: '<script src="<?=base_url().\'%s\'?>"></script>'
			},
			imgu: {
				src: imgsrc,
				tpl: '<img src="<?=base_url().\'%s\'?>" class="user-image" alt="User Image">'
			},
			imgcircle: {
				src: imgsrc,
				tpl: '<img src="<?=base_url().\'%s\'?>" class="img-circle" alt="User Image">'
			}
		}))
		.pipe(plugins.rename('frame.php'))
		.pipe(gulp.dest(DIST_PATH + '.standby/'));

	// 复制字符文件夹
	var fonts_matter = gulp.src('../../source/dist/css/fonts/*')
		.pipe(gulp.dest(DIST_PATH + '/fonts/'));

	// 图片处理
	var img_matter = gulp.src('../../source/dist/img/*')
		.pipe(gulp.dest(DIST_PATH + '/img/'));

	return merge([login_php, frame_php, fonts_matter, img_matter]);
});

/**
 *  内容功能页面的构建
 */
gulp.task('common.all.feauit',function(){
	// main & behind
	var main_php = gulp.src('../../source/template/main.html')
		.pipe(plugins.rename('main.php'));

	var behind_php = gulp.src('../../source/template/behind.html') 
		.pipe(plugins.rename('behind.php'));

	var user_php = gulp.src('../../source/template/users.html')
		.pipe(plugins.rename('users.php'));

	var components_php = gulp.src('../../source/template/components.html')
		.pipe(plugins.rename('components.php'));

	var groupusers_php = gulp.src('../../source/template/groupusers.html')
		.pipe(plugins.rename('groupusers.php'));

	var power_php = gulp.src('../../source/template/power.html')
		.pipe(plugins.rename('power.php'));

	var src_php = gulp.src('../../source/template/src.html')
		.pipe(plugins.rename('src.php'));

	var waf_rules_php = gulp.src('../../source/template/waf_rules.html')
		.pipe(plugins.rename('waf_rules.php'));

	var waf_php = gulp.src('../../source/template/waf.html')
		.pipe(plugins.rename('waf.php'));

	var demo_php = gulp.src('../../source/template/demo.html')
		.pipe(plugins.rename('demo.php'));

	return merge([main_php, behind_php, user_php, components_php,
		groupusers_php ,power_php,src_php,waf_rules_php,waf_php,demo_php])
		.pipe(gulp.dest('../../webapp/application/views/'));
});

gulp.task('common.all.feauit.model',function(){
	var hehind_modal = gulp.src('../../source/template/model/behind.html')
		.pipe(plugins.rename('behind.php'))
		.pipe(gulp.dest('../../webapp/application/views/model/'));

	var src_modal = gulp.src('../../source/template/model/src.html')
		.pipe(plugins.rename('src.php'))
		.pipe(gulp.dest('../../webapp/application/views/model/'));

	var src_modal = gulp.src('../../source/template/model/wafrule.html')
		.pipe(plugins.rename('wafrule.php'))
		.pipe(gulp.dest('../../webapp/application/views/model/'));

	var src_modal = gulp.src('../../source/template/model/waf.html')
		.pipe(plugins.rename('waf.php'))
		.pipe(gulp.dest('../../webapp/application/views/model/'));


	return hehind_modal;
});

/**
 *  写入带编号的静态资源（css/js）
 *  清理临时资源
 */
gulp.task('common.all.inject',['common.all.php'],function(){
	// 登陆页面
	var login_page = gulp.src([DIST_PATH + '/rev/***/**/*.json',DIST_PATH + '.standby/login.php']);

	// 控制台
	var main_page = gulp.src([DIST_PATH + '/rev/***/**/*.json',DIST_PATH + '.standby/frame.php']);

	return merge([login_page, main_page])
		.pipe(plugins.revCollector({
			replaceReved: true,
			removeComments: true
		}))
		.pipe(gulp.dest('../../webapp/application/views/'));
});

/**
 * 临时文件删除
 * 1、rev 配置
 * 2、standby 信息
 */
gulp.task('common.public.clean',['common.all.inject'],function(){
	return gulp.src([
			DIST_PATH + '.standby',
			DIST_PATH + 'rev'
		])
		.pipe(plugins.clean({ force: true }));
});

gulp.task('default',[
	'common.all.css',
	'common.all.js',
	'common.all.php',
	'common.all.feauit',
	'common.all.feauit.model',
	'common.all.inject',
	'common.public.clean'
]);
